package cn.lxg.controller.user;

import cn.lxg.pojo.TUser;
import cn.lxg.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller(value = "userUserController")
@RequestMapping(value = "/page/user")
public class UserController {
    @Autowired
    private final IUserService userService = null;

    @GetMapping(value = "/edit_user")
    public String editPage(HttpSession session, Model model) {
        // 准备数据
        TUser sessionUser = (TUser) session.getAttribute("user");
        TUser user = this.userService.get(sessionUser.getId());

        // 传递数据
        model.addAttribute("user", user);

        return "user/user/edit";
    }

    @PutMapping(value = "/user")
    public String edit(HttpSession session, Model model, TUser user) {
        // 准备数据
        TUser sessionUser = (TUser) session.getAttribute("user");

        // 准备数据
        user.setId(sessionUser.getId());
        String msg = this.userService.edit(user);

        // 传递数据
        model.addAttribute("msg", msg);

        return "common/message";
    }
}
